var curr_page = {
    'lens' : 0,
    'mounting' : 0,
    'sunglass' : 0,
    'presbyopic_glass' : 0,
    'contact_lens' : 0,
    'contact_lens_care_solution' : 0,
    'other' : 0
};
var curr_typeid = {
    'lens' : 10,
    'mounting' : 30,
    'sunglass' : 41,
    'presbyopic_glass' : 40,
    'contact_lens' : 20,
    'contact_lens_care_solution' : 50,
    'other' : 60
};
var query_data_list = {
    'lens' : {},
    'mounting' : {},
    'sunglass' : {},
    'presbyopic_glass' : {},
    'contact_lens' : {},
    'contact_lens_care_solution' : {},
    'other' : {}
};
var _selected = {};
var query_data = {};
$(function() {
    $(".datalist-header").remove();
    $(".datalist-length").remove();
    $(".control-icon.fa.fa-caret-down").remove();
    $(".menu-left").find("a").click(function(e){
        $(".menu-left").find("a").removeClass("active");
        $(this).addClass("active");
        $("form[data-type]").hide();
        $("form[data-type='"+$(this).attr("data-type")+"']").show();
    });
    $("input").on("keypress",function(e){
        if (e.which == 13) {
            query($(this).parents("form[data-type]").attr("data-type"));
        }
    });
    //光度传参绑定
    /*
    if(capp.data.param.type=="near"||capp.data.param.type=="far"){
        $(".content-box[data-type='lens']").find("[name='sph_left']").setData(capp.data.param.sph_left);
        $(".content-box[data-type='lens']").find("[name='sph_right']").setData(capp.data.param.sph_right);
        $(".content-box[data-type='lens']").find("[name='cyl_left']").setData(capp.data.param.cyl_left);
        $(".content-box[data-type='lens']").find("[name='cyl_right']").setData(capp.data.param.cyl_right);
        $(".content-box[data-type='presbyopic_glass']").find("[name='sph_left']").setData(capp.data.param.sph_left);
        $(".content-box[data-type='presbyopic_glass']").find("[name='sph_right']").setData(capp.data.param.sph_right);
    }else if(capp.data.param.type=="yinxing"){
        $(".content-box[data-type='contact_lens']").find("[name='sph_left']").setData(capp.data.param.sph_left);
        $(".content-box[data-type='contact_lens']").find("[name='sph_right']").setData(capp.data.param.sph_right);
        $(".content-box[data-type='contact_lens']").find("[name='cyl_left']").setData(capp.data.param.cyl_left);
        $(".content-box[data-type='contact_lens']").find("[name='cyl_right']").setData(capp.data.param.cyl_right);
    }else if(capp.data.param.type=="duojiaodian"){
        $(".content-box[data-type='lens']").find("[name='sph_left']").setData(capp.data.param.sph_left);
        $(".content-box[data-type='lens']").find("[name='sph_right']").setData(capp.data.param.sph_right);
        $(".content-box[data-type='lens']").find("[name='cyl_left']").setData(capp.data.param.cyl_left);
        $(".content-box[data-type='lens']").find("[name='cyl_right']").setData(capp.data.param.cyl_right);
        $(".content-box[data-type='presbyopic_glass']").find("[name='sph_left']").setData(capp.data.param.sph_left);
        $(".content-box[data-type='presbyopic_glass']").find("[name='sph_right']").setData(capp.data.param.sph_right);
        $(".content-box[data-type='presbyopic_glass']").find("[name='add_info_left']").setData(capp.data.param.add_info_left);
        $(".content-box[data-type='presbyopic_glass']").find("[name='add_info_right']").setData(capp.data.param.add_info_right);
    }*/
    $("a.cancel-btn").click(function(e){
       parent.layer.closeAll();
    });
    $("a.submit-btn").click(function(e){
        var data={};

        var selected_num = 0;
        $.each(_selected, function () {
            selected_num += 1;
        });
        if (selected_num == 0) {
            layer.msg("请选择商品");
            return;
        }
        rtn = _selected;

        for(var index in rtn){
            if (rtn[index].suggested_price == null
                || rtn[index].suggested_price == "null") {
                layer.alert("所选商品未定价,请定价！");
                return;
            }
        }

        for ( var index in rtn) {
            rtn[index].data_type = "goods";
            var arryString = [];

            rtn[index].name = productGroupName(rtn[index].Products);
            /************二级参数**********************/
            rtn[index].subparams = [];
            if (rtn[index].Products.TypeId == 10) {
                rtn[index].subparams.push(rtn[index].Snd.sph);
                rtn[index].subparams.push(rtn[index].Snd.cyl);
            } else if (rtn[index].Products.TypeId == 30) {
                rtn[index].subparams.push(rtn[index].Snd.model);
                rtn[index].subparams.push(rtn[index].Snd.color);
                if (rtn[index].Snd.size) {
                    rtn[index].subparams.push(rtn[index].Snd.size);
                }
            } else if (rtn[index].Products.TypeId == 41) {
                rtn[index].subparams.push(rtn[index].Snd.model);
                rtn[index].subparams.push(rtn[index].Snd.color);
                if (rtn[index].Snd.size) {
                    rtn[index].subparams.push(rtn[index].Snd.size);
                }
                if (rtn[index].Snd.function_info) {
                    rtn[index].subparams.push(rtn[index].Snd.function_info);
                }
            } else if (rtn[index].Products.TypeId  == 40) {
                rtn[index].subparams.push(rtn[index].Snd.sph);
                rtn[index].subparams.push(rtn[index].Snd.add);
            } else if (rtn[index].Products.TypeId  == 20) {
                rtn[index].subparams.push(rtn[index].Snd.sph);
                rtn[index].subparams.push(rtn[index].Snd.cyl);
            }
            parent.addGoods(rtn[index]);
        }
    });

    //获取配置
    $.post(get_product_options_url, {'fields':[]},  function(res){
        setattroption($('select[name="refractivity"]'),res.data.goods_lens_refractivity); //折射率
        setattroption($('select[name="optical_design"]'),res.data.goods_lens_optical_design); //光学设计
        setattroption($('select[name="function_info"]'),res.data.goods_function_info); //功效
        setattroption($('select[name="film"]'),res.data.goods_film); //膜层
        setattroption($('select[name="material_quality"]'),res.data.goods_frame_material_quality); //镜框材质
        setattroption($('select[name="spec"]'),res.data.goods_spec); //规格
        setattroption($('select[name="cast_away"]'),res.data.goods_cast_away); //佩戴周期
        setattroption($('select[name="care_solution_spec"]'),res.data.goods_care_solution_spec); //护理产品规格

    });
    function setattroption(obj,data) {
        var option = '<option value=""></option>';
        $.each(data, function(index, value) {
            option += '<option value="'+ value.val +'">'+ value.val +'</option>';
        });
        $(obj).html(option);
    }
});

function query(goods_type){
    var data=$("form[data-type='"+goods_type+"']").serializeObject();
    var reserve_num=[];
    reserve_num.push("1");
    data.goods_type=goods_type;
    data.reserve_num=reserve_num;

    if(goods_type=="lens"){
        delete data.lens_datalist;
    }else if(goods_type=="mounting"){
        delete data.mounting_datalist;
    }else if(goods_type=="sunglass"){
        delete data.sunglass_datalist;
    }else if(goods_type=="presbyopic_glass"){
        delete data.presbyopic_glass_datalist;
    }else if(goods_type=="contact_lens"){
        delete data.contact_lens_datalist;
    }else if(goods_type=="contact_lens_care_solution"){
        delete data.contact_lens_care_solution_datalist;
    }else if(goods_type=="other"){
        delete data.other_datalist;
    }
    curr_page[goods_type] = 0;
    query_data = data;
    get_database_inventory_list(goods_type);
}

function get_database_inventory_list(goods_type) {
    if(!goods_type) {
        goods_type = 'lens';
    }
    ajax_database_inventory_list(goods_type);
}

function ajax_database_inventory_list(goods_type) {
    query_data.offset = curr_page[goods_type] * query_data.limit;
    query_data.typeid = curr_typeid[goods_type];
    query_data.limit = 10;
    if(goods_type == 'lens' || goods_type == 'contact_lens') {
        query_data.limit = 7;
    }
    var box = $('form[data-type="'+ goods_type +'"]').find('.datalist-content');
    $.post(get_inventory_url, JSON.stringify(query_data), function (res) {
        if(curr_page[goods_type] == 0){
            get_database_pagination(goods_type, Math.ceil(res.iTotalDisplayRecords/query_data.limit));
            bind_selected_row_click(goods_type);
        }
        query_data_list[goods_type] = {};
        box.html('');
        $.each(res.aaData, function (index, value) {
            box.append(set_list_rows_html(goods_type, index, value));
        });
        set_default_selected_line(goods_type);
    }, 'json');
}

function set_default_selected_line(goods_type) {
    if(_selected) {
        var box = $('form[data-type="'+ goods_type +'"]');
        $.each(_selected, function (index, value) {
            box.find('.goods-info-row[data-index="'+ index +'"]').addClass("selected");
        });
    }
}

function set_list_rows_html(goods_type, index, data) {
    var html = [];
    html.push('<div class="goods-info-row" data-index="'+ data.Id +'">');
    html.push('<div class="item g-name"><span>'+ productGroupName(data.Products) +'</span></div>');
    switch (goods_type) {
        case 'lens':
            html.push('<div class="item">'+ data.Snd.sph +'</div>');
            html.push('<div class="item">'+ data.Snd.cyl +'</div>');
            break;
        case 'mounting':
           // html.push('<div class="item">'+ (data.Products.MaterialQuality ? data.Products.MaterialQuality : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Snd.model ? data.Snd.model : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Snd.color ? data.Snd.color : '&nbsp;') +'</div>');
            //html.push('<div class="item">'+ (data.Snd.size ? data.Snd.size : '&nbsp;') +'</div>');
            break;
        case 'sunglass':
            html.push('<div class="item">'+ (data.Snd.function_info ? data.Snd.function_info : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Snd.model ? data.Snd.model : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Snd.color ? data.Snd.color : '&nbsp;') +'</div>');
            //html.push('<div class="item">'+ (data.Snd.size ? data.Snd.size : '&nbsp;') +'</div>');
            break;
        case 'presbyopic_glass':
            html.push('<div class="item">'+ (data.Products.ModelNumber ? data.Products.ModelNumber : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Products.ColourNumber ? data.Products.ColourNumber : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ data.Snd.sph +'</div>');
            html.push('<div class="item">'+ data.Snd.add +'</div>');
            break;
        case 'contact_lens':
            html.push('<div class="item">'+ (data.Acceptance.expiry_date ? timetransDate(data.Acceptance.expiry_date) : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Products.Spec ? data.Products.Spec : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ data.Snd.sph +'</div>');
            html.push('<div class="item">'+ data.Snd.cyl +'</div>');
            break;
        case 'contact_lens_care_solution':
            html.push('<div class="item">'+ (data.Acceptance.expiry_date ? timetransDate(data.Acceptance.expiry_date) : '&nbsp;') +'</div>');
            html.push('<div class="item">'+ (data.Products.Spec ? data.Products.Spec : '&nbsp;') +'</div>');
            break;
    }
    if(data.price_id == 0) {
        html.push('<div class="item g-price color-red am-text-danger">￥暂未定价</div>');
    }else {
        html.push('<div class="item g-price color-red am-text-danger">￥'+ data.suggested_price +'</div>');
    }
    html.push('<div class="item am-text-danger">'+ data.purchase_num +'</div>');
    //html.push('<div class="item">&nbsp;</div>');
    html.push('</div>');

    if(query_data_list[goods_type]) {
        !query_data_list[goods_type][data.Id] ? query_data_list[goods_type][data.Id] = data : '';
    }

    return html.join('');
}

function bind_selected_row_click(goods_type) {
    var box = $('form[data-type="'+ goods_type +'"]');
    box.off('click').on('click', '.goods-info-row', function () {
        var _this = $(this);
        var id = $(this).data('index');
        if(_this.hasClass('selected')) {
            _this.removeClass('selected');
            delete _selected[id];
        } else {
            _this.addClass('selected');
            _selected[id] = query_data_list[goods_type][id];
        }
    });
}


function get_database_pagination(goods_type, count) {
    new Pagination({
        wrap: $('.am-pagination-'+goods_type), // 存放分页内容的容器
        count: count, // 总页数
        current: 1, // 当前的页数（默认为1）
        prevText: '上一页', // prev 按钮的文本内容
        nextText: '下一页', // next 按钮的文本内容
        callback: function(page) { // 每一个页数按钮的回调事件
            curr_page[goods_type] = page-1;
            ajax_database_inventory_list(goods_type);
        }
    });
}

